class InputController {
    constructor() {
        this.selectedPomodoros = 0; // 默认改为0（不设置）
        this.selectedDueDate = null;
    }

    setupPomodoroSelector() {
        const pomodoroBtn = document.getElementById('pomodoroBtn');
        const pomodoroDropdown = document.getElementById('pomodoroDropdown');
        const customPomodoro = document.getElementById('customPomodoro');

        // 点击按钮显示/隐藏下拉菜单
        pomodoroBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            pomodoroDropdown.classList.toggle('show');
            pomodoroBtn.classList.toggle('active');
        });

        // 点击选项
        document.querySelectorAll('.pomodoro-option').forEach(option => {
            option.addEventListener('click', () => {
                const count = parseInt(option.dataset.count);
                this.setPomodoros(count);
                this.closePomodoroDropdown();
            });
        });

        // 自定义输入
        customPomodoro.addEventListener('change', () => {
            const count = parseInt(customPomodoro.value);
            if (count >= 1 && count <= 20) {
                this.setPomodoros(count);
                this.closePomodoroDropdown();
            }
        });

        // 点击外部关闭下拉菜单
        document.addEventListener('click', () => {
            this.closePomodoroDropdown();
        });

        pomodoroDropdown.addEventListener('click', (e) => {
            e.stopPropagation();
        });
    }

    setPomodoros(count) {
        this.selectedPomodoros = count;
        const pomodoroCountElement = document.getElementById('pomodoroCount');
        if (count === 0) {
            pomodoroCountElement.textContent = '不设置';
        } else {
            pomodoroCountElement.textContent = count + '个番茄';
        }
        
        // 更新选中状态
        document.querySelectorAll('.pomodoro-option').forEach(option => {
            option.classList.remove('selected');
            if (parseInt(option.dataset.count) === count) {
                option.classList.add('selected');
            }
        });
    }

    closePomodoroDropdown() {
        document.getElementById('pomodoroDropdown').classList.remove('show');
        document.getElementById('pomodoroBtn').classList.remove('active');
    }

    setupDueDateSelector() {
        const dueDateBtn = document.getElementById('dueDateBtn');
        const dueDateDropdown = document.getElementById('dueDateDropdown');
        const customDueDate = document.getElementById('customDueDate');
        const clearDueDate = document.getElementById('clearDueDate');

        if (dueDateBtn && dueDateDropdown) {
            // 点击按钮显示/隐藏下拉菜单
            dueDateBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                dueDateDropdown.classList.toggle('show');
                dueDateBtn.classList.toggle('active');
            });

            // 快速选项
            document.querySelectorAll('.due-date-option').forEach(option => {
                option.addEventListener('click', (e) => {
                    const days = parseInt(e.target.dataset.days);
                    const dueDate = new Date();
                    dueDate.setDate(dueDate.getDate() + days);
                    this.setDueDate(dueDate);
                    dueDateDropdown.classList.remove('show');
                    dueDateBtn.classList.remove('active');
                });
            });

            // 自定义日期
            if (customDueDate) {
                customDueDate.addEventListener('change', (e) => {
                    if (e.target.value) {
                        const dueDate = new Date(e.target.value);
                        this.setDueDate(dueDate);
                        dueDateDropdown.classList.remove('show');
                        dueDateBtn.classList.remove('active');
                    }
                });
            }

            // 清除按钮
            if (clearDueDate) {
                clearDueDate.addEventListener('click', () => {
                    this.clearDueDate();
                    dueDateDropdown.classList.remove('show');
                    dueDateBtn.classList.remove('active');
                });
            }

            // 点击外部关闭
            document.addEventListener('click', (e) => {
                if (!dueDateBtn.contains(e.target) && !dueDateDropdown.contains(e.target)) {
                    dueDateDropdown.classList.remove('show');
                    dueDateBtn.classList.remove('active');
                }
            });
        }
    }

    setDueDate(date) {
        this.selectedDueDate = date;
        const dueDateBtn = document.getElementById('dueDateBtn');
        const dueDateText = document.getElementById('dueDateText');
        
        if (dueDateBtn && dueDateText) {
            dueDateBtn.classList.add('has-date');
            dueDateText.textContent = this.formatDueDate(date);
        }
    }

    clearDueDate() {
        this.selectedDueDate = null;
        const dueDateBtn = document.getElementById('dueDateBtn');
        const dueDateText = document.getElementById('dueDateText');
        const customDueDate = document.getElementById('customDueDate');
        
        if (dueDateBtn && dueDateText) {
            dueDateBtn.classList.remove('has-date');
            dueDateText.textContent = '到期时间';
        }
        
        if (customDueDate) {
            customDueDate.value = '';
        }
    }

    formatDueDate(date) {
        const today = new Date();
        const tomorrow = new Date(today);
        tomorrow.setDate(tomorrow.getDate() + 1);
        
        const dateStr = date.toDateString();
        const todayStr = today.toDateString();
        const tomorrowStr = tomorrow.toDateString();
        
        if (dateStr === todayStr) {
            return '今天';
        } else if (dateStr === tomorrowStr) {
            return '明天';
        } else {
            return date.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' });
        }
    }

    resetSelections() {
        // 重置番茄选择为不设置
        this.selectedPomodoros = 0;
        const pomodoroCount = document.getElementById('pomodoroCount');
        if (pomodoroCount) {
            pomodoroCount.textContent = '不设置';
        }
        
        // 重置到期时间选择
        this.clearDueDate();
    }
}

// 删除这行：module.exports = InputController;